package cn.edu.zafu.bigdata.x.sxy;

import java.util.Scanner;

public class ArithmeticExceptionDemo {
    // divide方法，用于计算两个整数的除法结果
    public static double divide(int a, int b) {
        if (b == 0) {
            // 当分母为0时，抛出ArithmeticException
            throw new ArithmeticException("除数不能为0");
        }
        return (double) a / b;
    }

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in); // 创建Scanner对象
        try {
            // 获取用户输入的分子
            System.out.print("请输入分子（numerator）: ");
            int numerator = scanner.nextInt();

            // 获取用户输入的分母
            System.out.print("请输入分母（denominator）: ");
            int denominator = scanner.nextInt();

            // 调用divide方法，并传入两个整数
            double result = divide(numerator, denominator);
            System.out.println("结果是: " + result);
        } catch (ArithmeticException e) {
            // 捕获ArithmeticException并输出错误信息
            System.out.println("发生错误: " + e.getMessage());
        } finally {
            // 关闭scanner
            scanner.close();
        }
    }
}